namespace Xmef.Serializer.Internal
{
    using System;
    using System.Collections;
    using System.Globalization;
    using System.Xml.Serialization;
    using Xmef.Library;

    [Serializable]
    internal class XmlFormatAttributes<T> : IXmlFormatAttributesInternal
    {
        private readonly Hashtable _attributes;
        [NonSerialized]
        private readonly XmlAttributeOverrides _overrides;
        [NonSerialized]
        private XmlSerializer _serializer;
        private readonly Type _type;

        public XmlFormatAttributes()
        {
            this._type = typeof(T);
            this._attributes = new Hashtable();
            this._overrides = new XmlAttributeOverrides();
        }

        public void Add(Type type, XmlAttributes attributes)
        {
            string key = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { type.Name });
            if (!this._attributes.ContainsKey(key))
            {
                this._attributes.Add(key, new XmlFormatterValue(attributes, type, key));
            }
        }

        public void Add(Type type, string member, XmlAttributes attributes)
        {
            string key = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { type.Name, member });
            if (!this._attributes.ContainsKey(key))
            {
                this._attributes.Add(key, new XmlFormatterValue(attributes, type, member));
            }
        }

        public XmlFormatterValue GetAttributes()
        {
            string key = string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { this._type.Name });
            if (!this._attributes.ContainsKey(key))
            {
                XmlAttributes attributes = new XmlAttributes();
                XmlFormatterValue format = new XmlFormatterValue(attributes, this._type, key);
                this._attributes[key] = format;
                return format;
            }
            return (XmlFormatterValue) this._attributes[key];
        }

        public XmlFormatterValue GetAttributes(string member)
        {
            string key = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { this._type.Name, member });
            if (!this._attributes.ContainsKey(key))
            {
                XmlAttributes attributes = new XmlAttributes();
                XmlFormatterValue format = new XmlFormatterValue(attributes, this._type, member);
                this._attributes[key] = format;
                return format;
            }
            return (XmlFormatterValue) this._attributes[key];
        }

        public XmlAttributeOverrides Overrides
        {
            get
            {
                return this._overrides;
            }
        }

        public XmlSerializer RuntimeSerializer
        {
            get
            {
                if (this._serializer == null)
                {
                    ICollection _keys = this._attributes.Keys;
                    if (_keys != null)
                    {
                        IEnumerator enumerator = _keys.GetEnumerator();
                        if (enumerator != null)
                        {
                            while (enumerator.MoveNext())
                            {
                                string member = enumerator.Current as string;
                                if (member != null)
                                {
                                    LogHelper.WriteOutSync(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", new object[] { this._type.Name, member }));
                                    XmlFormatterValue value = (XmlFormatterValue) this._attributes[member];
                                    if (string.Equals(member, string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { value.Type.Name, value.Member })) && (this._overrides[value.Type, value.Member] == null))
                                    {
                                        this._overrides.Add(value.Type, value.Member, value.Attributes);
                                    }
                                    if (string.Equals(member, string.Format(CultureInfo.InvariantCulture, "{0}", new object[] { value.Member })) && (this._overrides[value.Type] == null))
                                    {
                                        this._overrides.Add(value.Type, value.Attributes);
                                    }
                                }
                            }
                        }
                    }
                    this._serializer = new XmlSerializer(this._type, this._overrides);
                }
                return this._serializer;
            }
        }
    }
}

